package com.xinxin.intelligenceexam.presenter;

import com.orhanobut.logger.Logger;
import com.xinxin.intelligenceexam.activity.LoginActivity;
import com.xinxin.intelligenceexam.base.BaseView;
import com.xinxin.intelligenceexam.base.Constant;
import com.xinxin.intelligenceexam.contract.LoginContract;
import com.xinxin.intelligenceexam.domain.Student;
import com.xinxin.intelligenceexam.model.LoginModelImpl;
import com.xinxin.intelligenceexam.utils.GsonUtils;
import com.xinxin.intelligenceexam.utils.SharedPreferencesUtil;

import rx.Subscriber;

/**
 * Created by x9259 on 2016/10/26
 */

public class LoginPresenterImpl implements LoginContract.Presenter {
    LoginContract.View mView;
    LoginContract.Model mModel;

    public LoginPresenterImpl() {
        mModel = new LoginModelImpl();
    }

    @Override
    public void login(String userCode, String password) {
        mModel.login(new Subscriber<Student>() {
            @Override
            public void onCompleted() {
                mView.gotoMain();
            }

            @Override
            public void onError(Throwable e) {
                Logger.d(e);
            }

            @Override
            public void onNext(Student student) {
                SharedPreferencesUtil.setString(((LoginActivity) mView), Constant.SHAREPREFENCE,Constant.SP_STUDENT, GsonUtils.toJsonString(student));
                Logger.d(student);
            }
        }, userCode, password);

    }

    @Override
    public void attachView(BaseView view) {
        mView = (LoginContract.View) view;
    }
}